package com.crudpro.javacrud.config;

import com.crudpro.javacrud.model.User;
import com.crudpro.javacrud.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class DataInitializer {

    private final UserRepository userRepository;

    @Bean
    public CommandLineRunner initData() {
        return args -> {
            // Check if test user already exists
            if (!userRepository.findByUsername("admin").isPresent()) {
                // Create a test user
                User adminUser = new User();
                adminUser.setUsername("admin");
                adminUser.setEmail("admin@example.com");
                adminUser.setPassword("admin123"); // In a real application, encrypt this password
                
                // Save the test user
                userRepository.save(adminUser);
                
                System.out.println("Test user 'admin' created with password 'admin123'");
            } else {
                System.out.println("Test user 'admin' already exists");
            }
        };
    }
} 